# -*- encoding: utf-8 -*-
"""
SDI导入任务调度生成器.py
Created on 2025/10/10 15:58
Copyright (c) 2025/10/10, dt_shuke 版权所有.
@author: ghostwowo
"""
import json

# 作业归属人
import os
import time
import zipfile
import random

owner = 'guoziqing'
#作业归属目录
directory = '/PRD/PRD_SDI/ICS'

# 调度集群名称
clustername = 'cdm-hq04'

start_time = time.strftime('%Y-%m-%dT%H:%M:%S+08')

with open(r"C:\Users\Administrator\Desktop\大唐数科\一体化定时任务.txt",'r',encoding= 'utf8') as f1:
    line = f1.readline()
    while line != '':
        strs = line.replace("\n","").split("\t")
        #调度 作业名
        task_name = strs[0]
        #节点名称，脚本名或者CDM名
        sdi_task = strs[1]

        hour = random.randint(4, 6)
        minute = random.randint(0, 59)
        second = random.randint(0, 59)

        with open(f"../job_directory/{task_name}.job", "w") as out:
            with open("sdi导入任务调度template模板", "r") as f:
                jsonobj = json.load(f)
                jsonobj['basicConfig']['owner'] = owner
                jsonobj['directory'] = directory
                jsonobj['name'] = task_name
                node = jsonobj['nodes'][0]
                node['name'] = sdi_task
                properties = node['properties']
                properties.append({"name": "clusterName", "value": clustername})
                properties.append({"name": "jobName", "value": sdi_task})
                jsonobj['schedule']['cron']['expression'] = f"{second} {minute} {hour} * * ?"
                jsonobj['schedule']['cron']['startTime'] = start_time
                json.dump(jsonobj, out)

        line = f1.readline()



with zipfile.ZipFile("../job_zip/job.zip",'w',zipfile.ZIP_DEFLATED) as zip:
    files = os.listdir("../job_directory")
    for f in files:
        zip.write("../job_directory/"+f,os.path.relpath(f,os.getcwd()))
